package org.example.zhihuan.services;

import org.example.zhihuan.util.Db;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class OrderRecord {
private int orderId;
private int goodsId;
private int userId;


private boolean isSaved = false;



    public int getOrderId() {
        return orderId;
    }

    public int getGoodsId() {
        return goodsId;
    }

    public int getUserId() {
        return userId;
    }

    /**
     * 持久化
     */
    private void save(){
        StringBuilder sb = new StringBuilder();
        if(isSaved){
            sb.append("update record set ")
                    .append(" order_id = ").append(orderId).append(",")
                    .append(" goods_id = ").append(goodsId).append(",")
                    .append(" user_id = ").append(userId);
        }else {
            sb.append("insert into record (order_id,goods_id,user_id) values (")
                    .append(orderId).append(",").append(goodsId)
                    .append(",").append(userId).append(")");
        }
        isSaved = true;
    }

    /**
     * 创建订单明晰
     * @param orderId
     * @param goodId
     * @param userId
     * @return
     */
    public OrderRecord createOrder(int orderId,int goodId,int userId){
        OrderRecord or = new OrderRecord();
        or.orderId = orderId;
        or.goodsId = goodId;
        or.userId = userId;
        or.save();
        return or;
    }

    /**
     * get订单明细
     * @param orderId
     * @param userId
     * @return
     */
    public static List<OrderRecord> getRecord(int orderId,int userId){
        List<OrderRecord> recordList = new ArrayList<>();
        Db.read("select * from record where order_id='" + orderId + "'" + "and user_id='" + userId + "'",set -> {
            while (set.next()){
                OrderRecord record = new OrderRecord();
                record.orderId = set.getInt("order_id");
                record.userId = set.getInt("user_id");
                record.goodsId = set.getInt("goods_id");
                recordList.add(record);
            }
            return null;
        });
        return recordList;
    }




    public void setGoodsId(int goodId) {
        this.goodsId = goodId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }
}
